package com.hfjyz.payment.bean;

import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

import com.hfjyz.payment.constant.Constant;

public class PaySearchBean {
    private static String tranCode = null;
    private static String cis = null;
    private static String bankCode = null;
    private static String id = null;

    private static String shopType = null;
    private static String shopCode = null;
    private static String shopAcct = null;
    private static String qrySerialNo = null;
    private static String qryOrderNum = null;
    private static String beginDate = null;
    private static String endDate = null;
    private static String beginTime = null;
    private static String endTime = null;

    private String fSeqno = null;
    
    static {
        init();
    }
    
    private static void init() {
        InputStream in = null;
        try {
            in = PaySearchBean.class.getResourceAsStream("/condition.properties");
            Properties p = new Properties();
            p.load(in);

            tranCode = p.getProperty(Constant.TRANS_CODE);
            cis = p.getProperty(Constant.CIS);
            bankCode = p.getProperty(Constant.BANK_CODE);
            id = p.getProperty(Constant.ID);
            shopType = p.getProperty(Constant.SHOP_TYPE);
            shopCode = p.getProperty(Constant.SHOP_CODE);
            shopAcct = p.getProperty(Constant.SHOP_ACCT);
            qrySerialNo = p.getProperty(Constant.QRY_SN);
            qryOrderNum = p.getProperty(Constant.QRY_ORDER_NUM);
            beginDate = p.getProperty(Constant.BEGIN_DATE);
            endDate = p.getProperty(Constant.END_DATE);
            beginTime = p.getProperty(Constant.BEGIN_TIME);
            endTime = p.getProperty(Constant.END_TIME);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -1);
            String nowDate = sdf.format(cal.getTime());
            if (beginDate == null || beginDate.length() == 0) {
                beginDate = nowDate;
            }
            
            if (endDate == null || endDate.length() == 0) {
                endDate = nowDate;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }

    public String getRequestXml(String nextTag) {
        if (nextTag == null) {
            nextTag = "";
        }
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Random r = new Random();
        fSeqno = format.format(new Date()) + String.format("%04d", r.nextInt(1000));

        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding =\"GBK\"?>");
        sb.append("<CMS>");
        sb.append("<eb>");
        sb.append("<pub>");
        sb.append("<TransCode>").append(tranCode).append("</TransCode>");
        sb.append("<CIS>").append(cis).append("</CIS>");
        sb.append("<BankCode>").append(bankCode).append("</BankCode>");
        sb.append("<ID>").append(id).append("</ID>");
        sb.append("<TranDate>").append(fSeqno.substring(0, 8)).append("</TranDate>");
        sb.append("<TranTime>").append(fSeqno.substring(8, 14) + "000").append("</TranTime>");
        sb.append("<fSeqno>").append(fSeqno).append("</fSeqno>");
        sb.append("</pub>");
        sb.append("<in>");
        sb.append("<ShopType>").append(shopType).append("</ShopType>");
        sb.append("<ShopCode>").append(shopCode).append("</ShopCode>");
        sb.append("<ShopAcct>").append(shopAcct).append("</ShopAcct>");
        sb.append("<QrySerialNo>").append(qrySerialNo).append("</QrySerialNo>");
        sb.append("<QryOrderNum>").append(qryOrderNum).append("</QryOrderNum>");
        sb.append("<BeginDate>").append(beginDate).append("</BeginDate>");
        sb.append("<EndDate>").append(endDate).append("</EndDate>");
        sb.append("<BeginTime>").append(beginTime).append("</BeginTime>");
        sb.append("<EndTime>").append(endTime).append("</EndTime>");
        sb.append("<ResultType>010</ResultType>");
        sb.append("<NextTag>").append(nextTag).append("</NextTag>");
        sb.append("<ReqReserved1></ReqReserved1>");
        sb.append("<ReqReserved2></ReqReserved2>");
        sb.append("</in>");
        sb.append("</eb>");
        sb.append("</CMS>");

        return sb.toString();
    }

    public static String getTranCode() {
        return tranCode;
    }

    public static void setTranCode(String tranCode) {
        PaySearchBean.tranCode = tranCode;
    }

    public static String getCis() {
        return cis;
    }

    public static void setCis(String cis) {
        PaySearchBean.cis = cis;
    }

    public static String getBankCode() {
        return bankCode;
    }

    public static void setBankCode(String bankCode) {
        PaySearchBean.bankCode = bankCode;
    }

    public static String getId() {
        return id;
    }

    public static void setId(String id) {
        PaySearchBean.id = id;
    }

    public static String getShopType() {
        return shopType;
    }

    public static void setShopType(String shopType) {
        PaySearchBean.shopType = shopType;
    }

    public static String getShopCode() {
        return shopCode;
    }

    public static void setShopCode(String shopCode) {
        PaySearchBean.shopCode = shopCode;
    }

    public static String getShopAcct() {
        return shopAcct;
    }

    public static void setShopAcct(String shopAcct) {
        PaySearchBean.shopAcct = shopAcct;
    }

    public static String getQrySerialNo() {
        return qrySerialNo;
    }

    public static void setQrySerialNo(String qrySerialNo) {
        PaySearchBean.qrySerialNo = qrySerialNo;
    }

    public static String getQryOrderNum() {
        return qryOrderNum;
    }

    public static void setQryOrderNum(String qryOrderNum) {
        PaySearchBean.qryOrderNum = qryOrderNum;
    }

    public static String getBeginDate() {
        return beginDate;
    }

    public static void setBeginDate(String beginDate) {
        PaySearchBean.beginDate = beginDate;
    }

    public static String getEndDate() {
        return endDate;
    }

    public static void setEndDate(String endDate) {
        PaySearchBean.endDate = endDate;
    }

    public static String getBeginTime() {
        return beginTime;
    }

    public static void setBeginTime(String beginTime) {
        PaySearchBean.beginTime = beginTime;
    }

    public static String getEndTime() {
        return endTime;
    }

    public static void setEndTime(String endTime) {
        PaySearchBean.endTime = endTime;
    }

    public String getfSeqno() {
        return fSeqno;
    }

    public void setfSeqno(String fSeqno) {
        this.fSeqno = fSeqno;
    }

}
